home *** CD-ROM | disk | FTP | other *** search
- /* RCS -- $Header: /u5/dvadura/src/public/dmake/src/RCS/path.c,v 1.1 1994/10/06 17:41:58 dvadura Exp $
- -- SYNOPSIS -- pathname manipulation code
- --
- -- DESCRIPTION
- -- Pathname routines to handle building and pulling appart
- -- pathnames.
- --
- -- AUTHOR
- -- Dennis Vadura, dvadura@watdragon.uwaterloo.ca
- -- CS DEPT, University of Waterloo, Waterloo, Ont., Canada
- --
- -- COPYRIGHT
- -- Copyright (c) 1992,1994 by Dennis Vadura. All rights reserved.
- --
- -- This program is free software; you can redistribute it and/or
- -- modify it under the terms of the GNU General Public License
- -- (version 1), as published by the Free Software Foundation, and
- -- found in the file 'LICENSE' included with this distribution.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warrant of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program; if not, write to the Free Software
- -- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- --
- -- LOG
- -- $Log: path.c,v $
- * Revision 1.1 1994/10/06 17:41:58 dvadura
- * dmake Release Version 4.0, Initial revision
- *
- */
-
- #include "extern.h"
-
-
- /*
- ** Return the suffix portion of a filename, assumed to begin with a `.'.
- */
- PUBLIC char *
- Get_suffix(name)
- char *name;
- {
- char *suff;
-
- if(name == NIL(char) || (suff = strrchr(name, '.')) == NIL(char))
- suff = ".NULL";
-
- return (suff);
- }
-
-
- PUBLIC char *
- Basename(path)
- char *path;
- {
- char *p;
- char *q;
-
- if( path && *(q = path) ) {
- for(; *(p=DmStrPbrk(q, DirBrkStr)) != '\0'; q = p+1 );
- if( !*q ) {
- for( p=q-1; p != path; --p )
- if( strchr( DirBrkStr, *p ) == NIL(char) ) return( p+1 );
- return( strchr(DirBrkStr, *p)?path:(p+1) );
- }
- path = q;
- }
- return( path );
- }
-
-
- PUBLIC char *
- Filedir(path)
- char *path;
- {
- char *p;
- char *q;
-
- if( path && *(q = path) ) {
- for(; *(p=DmStrPbrk(q,DirBrkStr)) != '\0'; q=p+1 );
-
- if (q == path) return("");
-
- for(p=q-1; p!=path; --p)
- if( strchr(DirBrkStr,*p) == NIL(char) )
- break;
-
- p[1] = '\0';
- }
-
- return(path);
- }
-
-
-
- /*
- ** Take dir and name, and return a path which has dir as the directory
- ** and name afterwards.
- **
- ** N.B. Assumes that the dir separator string is in DirSepStr.
- ** Return path is built in a static buffer, if you need to use it
- ** again you must strdup the result returned by Build_path.
- */
- PUBLIC char *
- Build_path(dir, name)
- char *dir;
- char *name;
- {
- register char *p;
- register char *q;
- static char *path = NIL(char);
- static unsigned buflen = 0;
- int plen = 0;
- int dlen = 0;
- int len;
-
- if( dir != NIL(char) ) dlen = strlen( dir );
- if( name != NIL(char) ) plen = strlen( name );
- #ifdef _MPW
- if (dir[dlen-1] == ':' && name[0] == ':') {
- --plen;
- ++name;
- }
- #endif
- len = plen+dlen+strlen(DirSepStr)+1;
-
- if( len > buflen ) {
- buflen = (len+16) & ~0xf; /* buf is always multiple of 16 */
-
- if( path == NIL(char) )
- path = MALLOC( buflen, char );
- else
- path = realloc( path, (unsigned) (buflen*sizeof(char)) );
- }
-
- *path = '\0';
-
- if( dlen ) {
- strcpy( path, dir );
- if( *path && strchr(DirBrkStr, dir[dlen-1]) == NIL(char) )
- strcat( path, DirSepStr );
- }
-
- if ( plen ) {
- while ( *name && strchr(DirBrkStr,*name) != 0 ) name++;
- strcat( path, name );
- }
-
- q=path;
- while( *q ) {
- char *t;
-
- p=DmStrPbrk(q,DirBrkStr);
- t=DmStrPbrk(p+1,DirBrkStr);
- if( !*p || !*t ) break;
-
- if ( p-q == 1 && *q == '.' ) {
- strcpy(q,DmStrSpn(p,DirBrkStr));
- q = path;
- }
- else if (
- !(p-q == 2 && strncmp(q,"..",2) == 0)
- && (t-p-1 == 2 && strncmp(p+1,"..",2) == 0)
- ) {
- strcpy(q,DmStrSpn(t,DirBrkStr));
- q = path;
- }
- else
- q = p+1;
- }
-
- return( path );
- }
-